home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7410 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.0 KB  |  38 lines

  1. Path: chaos.kulnet.kuleuven.ac.be!usenet
  2. From: Andreas De Troy <Andreas.DeTroy@ped.kuleuven.ac.be>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help me elect rounding of int division
  5. Date: 26 Feb 1996 09:09:00 GMT
  6. Organization: KUL
  7. Message-ID: <4grtbc$t9f@chaos.kulnet.kuleuven.ac.be>
  8. NNTP-Posting-Host: pcip194.psy.kuleuven.ac.be
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
  13.  
  14. news:4gq83r$omb@solutions.solon.com
  15. swedecj@vcnet.com (Carl Jacobson) wrote:
  16. >xmsb@shadow.borland.com (Maurice S. Barnum) wrote:
  17. >
  18. >>swedecj@vcnet.com (Carl Jacobson) writes:
  19. >
  20. >>>Please help me solve this task.
  21. >
  22. >>>I have now tried (Borland C++ version 3.0) for two solid days to write
  23. >>>a function that would allow me to round the quotient of (a/b) up or
  24. >>>down based on the "nearest" integer instead of being truncated to the
  25. >>>smallest.
  26.  
  27. How about this:
  28.  
  29. #include <float.h>
  30.  
  31. long rounddiv (long a, long b)
  32. {
  33.     return ((long) (floor (((float) a / b) + 0.5)));
  34. }
  35.  
  36. It uses floating point, of course.
  37.  
  38.